package com.example.fuzhuang.services;

import com.example.fuzhuang.dto.Type;
import com.example.fuzhuang.dto.TypeExample;
import com.example.fuzhuang.mapper.TypeMapper;
import com.example.fuzhuang.mapper.TypeMyMapper;
import com.example.fuzhuang.req.TypeReq;
import com.example.fuzhuang.resp.TypeResp;
import com.example.fuzhuang.utils.CopyUtil;
import com.example.fuzhuang.utils.MenuUtil;
import com.example.fuzhuang.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TypeService {
    private static final Logger LOG = LoggerFactory.getLogger(TypeService.class);

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private TypeMyMapper typeMyMapper;

    @Resource
    private SnowFlake snowFlake;

    public TypeResp save(TypeReq req){

        Type type = CopyUtil.copy(req, Type.class);
        if(req.getId() == null){
            Long id = snowFlake.nextId();
            type.setId(id);

            TypeExample typeExample = new TypeExample();

            TypeExample.Criteria criteria = typeExample.createCriteria();

            if(req.getParentId() != null) {
                criteria.andParentIdEqualTo(req.getParentId());
            }else{
                criteria.andParentIdIsNull();
            }

            List<Type> typeDatas = typeMapper.selectByExample(typeExample);
            if(typeDatas.size() > 0) {
                Type menusLastData = typeDatas.get(typeDatas.size()-1);
                type.setSort(menusLastData.getSort()+1);
            }else{
                type.setSort(1);
            }

            typeMapper.insert(type);
        }else {
            typeMapper.updateByPrimaryKeySelective(type);
        }

        TypeResp typeResp =  CopyUtil.copy(req, TypeResp.class);
        return typeResp;

    }

    public List<Type> batchUpdate(List<TypeReq> req){

        List<Type> types =  CopyUtil.copyList(req, Type.class);
        typeMyMapper.batchUpdate(types);

        return types;
    }

    public List<TypeResp> all(TypeReq req) {
        TypeExample typeExample = new TypeExample();
        typeExample.setOrderByClause("sort desc");
        TypeExample.Criteria criteria = typeExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike('%'+req.getName()+'%');
        }

        List<Type> types = typeMapper.selectByExample(typeExample);

        List<TypeResp> typeResp =  CopyUtil.copyList(types,TypeResp.class);
        List<TypeResp> res = new ArrayList<>();

        for (TypeResp type : typeResp){
            if(type.getParentId() == null) {

                type.setChildren(new MenuUtil().findChilds(type.getId(),typeResp));
                res.add(type);
            }
        }

        return res;
    }

    public void delete(TypeReq req){
        typeMapper.deleteByPrimaryKey(req.getId());

        TypeExample typeExample = new TypeExample();

        TypeExample.Criteria criteria = typeExample.createCriteria();

        criteria.andParentIdEqualTo(req.getId());

        typeMapper.deleteByExample(typeExample);
    }
}
